$(function () {
    var Controller = {
        table: $("#table"),
        init: function () {
            this.initTable();
            this.bindEvent();
        },
        initTable: function () {
            this.table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [{
                    field: 'id',
                    title: 'ID'
                }, {
                    field: 'name',
                    title: '姓名'
                }, {
                    field: 'mobile',
                    title: '手机号'
                }, {
                    field: 'school.name',
                    title: '所属学校'
                }, {
                    field: 'community.name',
                    title: '所属社区'
                }, {
                    field: 'teaching_years',
                    title: '教龄'
                }, {
                    field: 'status',
                    title: '状态',
                    formatter: function (value) {
                        return ['待审核', '已通过', '已拒绝'][value];
                    }
                }, {
                    field: 'operate',
                    title: '操作',
                    formatter: Controller.operateFormatter
                }]
            });
        },
        operateFormatter: function (value, row, index) {
            return [
                '<a href="javascript:;" class="btn btn-xs btn-info btn-edit" title="编辑">',
                '<i class="fa fa-pencil"></i>',
                '</a>'
            ].join('');
        },
        bindEvent: function () {
            $(document).on('click', '.btn-approve', function () {
                var ids = Controller.table.bootstrapTable('getSelections').map(function (row) {
                    return row.id;
                });
                if (ids.length === 0) {
                    Layer.alert('请选择要审核的记录');
                    return;
                }
                Fast.api.ajax({
                    url: 'community/project/teacher/approve',
                    data: {ids: ids}
                }, function () {
                    Controller.table.bootstrapTable('refresh');
                });
            });
        }
    };
    Controller.init();
});